/*
 * @(#)FileTree.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.mainGUI.fileDirectory;

import java.io.File;

import java.util.ArrayList;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class FileTree extends JTree implements TreeSelectionListener {

    /**
     * Constructs ...
     *
     */
    public FileTree() {
        super();
    }

    /**
     * Constructs ...
     *
     *
     * @param file
     */
    public FileTree(File file) {
        ArrayList listFolder = new ArrayList(),
                  listFile   = new ArrayList();
        String[]  m_dir      = file.list();
        File      file1;
        FileNode  fileNode = new FileNode(file.getName(), true, file);
        FileNode  fileNodeFolder, fileNodeFile;

        for (int i = 0; i < m_dir.length; i++) {
            file1 = new File(file.getPath() + "\\" + m_dir[i]);

            if (file1.isDirectory()) {
                listFolder.add(m_dir[i]);
                fileNodeFolder = new FileNode(m_dir[i], true, file1);
                fileNodeFolder = addFolder(file1);
                fileNode.add(fileNodeFolder);
            }
        }

        for (int i = 0; i < m_dir.length; i++) {
            file1 = new File(file.getPath() + "\\" + m_dir[i]);

            {
                listFile.add(m_dir[i]);
                fileNodeFile = new FileNode(m_dir[i], true, file1);
                fileNode.add(fileNodeFile);
            }
        }

        setModel(new DefaultTreeModel(fileNode));
        this.setCellRenderer(new FileTreeCellRender());
    }

    /**
     * Constructs ...
     *
     *
     * @param root
     */
    public FileTree(TreeNode root) {
        super(root);
    }

    public FileNode addFolder(File file) {
        ArrayList listFolder = new ArrayList(),
                  listFile   = new ArrayList();
        String[]  m_dir      = file.list();
        File      file1;
        FileNode  fileNode = new FileNode(file.getName(), true, file);
        FileNode  fileNodeFolder, fileNodeFile;

        for (int i = 0; i < m_dir.length; i++) {
            file1 = new File(file.getPath() + "\\" + m_dir[i]);

            if (file1.isDirectory()) {
                listFolder.add(m_dir[i]);
                fileNodeFolder = new FileNode(m_dir[i], true, file1);
                fileNodeFolder = addFolder(file1);
                fileNode.add(fileNodeFolder);
            }
        }

        for (int i = 0; i < m_dir.length; i++) {
            file1 = new File(file.getPath() + "\\" + m_dir[i]);

            {
                listFile.add(m_dir[i]);
                fileNodeFile = new FileNode(m_dir[i], true, file1);
                fileNode.add(fileNodeFile);
            }
        }

        return fileNode;
    }

    public void valueChanged(TreeSelectionEvent arg0) {

        // TODO Auto-generated method stub
//      add code to expand this tree node
    }
}
